package com.jia.springcloud.controller;

import com.jia.springcloud.pojo.Dept;
import com.jia.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 1038940655
 * @create 2021/1/13 17:35
 */

//提供RestFul服
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;
    
//    获取一些配置信息具体到一些微服务
    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping("/dept/queryDeptController")
    public List<Dept> queryDept(){
        return deptService.queryDept();
    }

    @RequestMapping("/dept/queryDeptById/{deptno}")
    public Dept queryDeptById(@PathVariable("deptno") Integer deptno){
        return deptService.queryDeptById(deptno);
    }

//    获取注册进来的微服务的一些信息
    @RequestMapping("/dept/discovery")
    public Object discoveryClient(){
//        获取微服务列表的清单
        List<String> services = discoveryClient.getServices();
        System.out.println("discovery=>services"+services);

//        得到一些微服务信息，通过具体的微服务id，applicationName
        List<ServiceInstance> instances = discoveryClient.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
        for (ServiceInstance instance : instances) {
            System.out.println(instance.getHost()+"\t"+
                               instance.getInstanceId()+"\t"+
                               instance.getPort()+"\t"+
                               instance.getUri());
        }
        return this.discoveryClient;
    }
}
